package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回查询背包项消息
 */
public class Backpack_SearchReturnProto implements IProto {
    private static MMO_DataStream dataStream = new MMO_DataStream();
    @Override
    public short getProtoCode() { return 16005; }
    public Integer BackpackItemCount; //背包项数量
    public ArrayList<BackpackItem> ItemList; //背包项
    public static class BackpackItem {
        public Integer BackpackItemId; //背包项编号
        public byte GoodsType; //物品类型
        public Integer GoodsId; //物品基础编号
        public Integer GoodsServerId; //物品服务器端编号
        public Integer GoodsOverlayCount; //物品叠加数量
    }
    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(BackpackItemCount);
            for (int i = 0; i < BackpackItemCount; i++) {
                dataStream.WriteInt(ItemList.get(i).BackpackItemId);
                dataStream.WriteByte(ItemList.get(i).GoodsType);
                dataStream.WriteInt(ItemList.get(i).GoodsId);
                dataStream.WriteInt(ItemList.get(i).GoodsServerId);
                dataStream.WriteInt(ItemList.get(i).GoodsOverlayCount); } } catch (IOException e) {
            e.printStackTrace(); }
        return out.toByteArray();
    }

    public static Backpack_SearchReturnProto GetProto(byte[] buffer) {
        Backpack_SearchReturnProto proto = new Backpack_SearchReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.BackpackItemCount = dataStream.ReadInt();
            proto.ItemList = new ArrayList<BackpackItem>();
            for (int i = 0; i < proto.BackpackItemCount; i++) {
                BackpackItem _Item = new BackpackItem();
                _Item.BackpackItemId = dataStream.ReadInt();
                _Item.GoodsType = (byte)dataStream.ReadByte();
                _Item.GoodsId = dataStream.ReadInt();
                _Item.GoodsServerId = dataStream.ReadInt();
                _Item.GoodsOverlayCount = dataStream.ReadInt();
                proto.ItemList.add(_Item);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}